#define _GNU_SOURCE
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <pthread.h>
#include <sys/types.h>
#include <sys/syscall.h>

void *test_thread(void *arg)
{
	pthread_t tid = pthread_self();
	pid_t pid = getpid();
	pid_t pidt = syscall(SYS_gettid);

	printf("tid = %ld,pid = %ld,pidt = %ld\n",(long)tid,(long)pid,(long)pidt);
	return NULL;
}

void test_ios_item(void)
{
    FILE *fp = fopen("apple.list","r");
    if(fp==NULL)
	return ;
    char *line = NULL;
    size_t len;
    int readlen;

    while((readlen=getline(&line,&len,fp))!=-1)
    {
	if(len > 7)
	{
	    char length[16]={0};
	    int dlen=0,plen=0,tlen=0;
	    memcpy(length,line,7);
	    sscanf(length,"%02d%02d%03d",&dlen,&plen,&tlen);
	    if(dlen>0 && plen>0 && tlen>0 && dlen<32 && plen<32 && tlen<128)
	    {
		char format[16]={0};
		char did[32]={0};
		char psw[32]={0};
		char token[128]={0};
		snprintf(format,sizeof(format),"%%%ds%%%ds%%%ds",dlen,plen,tlen);
		sscanf(line+7,format,did,psw,token);
		printf("----format:%s,did:%s,psw:%s,token:%s\n",format,did,psw,token);
	    }
	}
    }

    if(line) free(line);
    fclose(fp);
}

int main(int argc,char *argv[])
{
	int ret = 0;
#if 0
	pid_t pid = getpid();
	printf("pid = %ld\n",(long)pid);

	pthread_t t1,t2;
	pthread_create(&t1,NULL,test_thread,NULL);
	pthread_create(&t2,NULL,test_thread,NULL);

	pthread_join(t1,NULL);
	pthread_join(t2,NULL);
	ret = strncmp("1234","123456",16);
	printf(" ret = %d\n",ret);
#endif
	test_ios_item();
	return ret;
}

